<template>
    <div id="add" style="padding: 0 15px;">
        <el-row class="mt20">
            <el-col :span="24">
                <el-input placeholder="请填写手机号(默认密码也为此账号)！" v-model="username" class="mt mb">
                    <template slot="prepend">管理员账号</template>
                </el-input>
            </el-col>
        </el-row>
        <el-row class="mt20">
            <el-col :span="24">
                <el-input placeholder="请输管理员姓名！" v-model="name">
                    <template slot="prepend">管理员姓名</template>
                </el-input>
            </el-col>
        </el-row>
        <el-row class="mt20">
            <el-col :span="24">
                <el-input type="textarea" placeholder="请输管理员描述信息！" v-model="remarks" class="mt" v-show="!id"></el-input>
            </el-col>
        </el-row>
        <el-row class="mt20">
            <el-col :span="12">
                <el-button @click.native="close" style="width:90%;display:block;">取消</el-button>
            </el-col>
            <el-col :span="12">
                <el-button type="success" @click.native="submit" style="width:90%;display:block;" :loading="loading">确定</el-button>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'addAdmin',
  data () {
    return {
      loading: false,
      id: 0,
      username: '',
      name: '',
      remarks: '',
      shopid: ''
    }
  },
  async mounted () {
    let RequestUrl = C.getDatas()
    if (RequestUrl.shopid) {
      this.shopid = RequestUrl.shopid
    }
    if (RequestUrl.row) {
      let row = RequestUrl.row
      this.id = row.id
      this.username = row.username
      this.name = row.name
      this.remarks = row.remarks
      this.shopid = row.departmentid
    }
  },
  methods: {
    async submit (index) {
      if (!(/^1[345678]\d{9}$/.test(this.username))) {
        alert('手机格式不正确')
        return false
      }
      if (/^\s*$/.test(this.name)) {
        alert('姓名不能为空')
        return false
      }
      let data = {
        id: this.id,
        username: this.username,
        name: this.name,
        remarks: this.remarks,
        departmentid: this.shopid
      }
      this.loading = true
      let res = await C.ajax('/organization/shop/addadmin', data, 'post')
      this.loading = false
      if (res) {
        C.closeBrowserWindow(res)
      }
    },
    close () {
      C.closeBrowserWindow()
    }
  }
}
</script>
<style>
.mt20 {
  margin-top: 20px;
}
</style>
